<template>
	<view class="container">
		<view class="form">
			<uni-forms ref="form" :modelValue="form" label-position='right' label-width="80">
				<uni-forms-item :label="`${$t('store.form.name')}`">
					<uni-easyinput v-model="form.name" disabled />
				</uni-forms-item>
				<uni-forms-item :label="`${$t('store.form.batchnumber')}`">
					<uni-easyinput v-model="form.batchnumber" disabled />
				</uni-forms-item>
				<uni-forms-item :label="`${$t('store.form.expiryDate')}`">
					<uni-easyinput v-model="form.expiryDate" disabled />
				</uni-forms-item>
				<uni-forms-item :label="`${$t('store.form.num')}`">
					<UniNumberBox 
					:value="form.num" 
					@change="numchange" 
					 :min="1"
					 @foucs="formfocus"
					 @blur="formblur"
					 :focus="numFocus"
					:max="form.totalNum">
					</UniNumberBox>
				</uni-forms-item>
				<uni-forms-item :label="`${$t('store.form.storeNum')}`">
					<view>
						<uni-easyinput v-model="form.storeNum" disabled="" />
					</view>
				</uni-forms-item>
				<uni-forms-item :label="`${$t('store.form.totalNum')}`">
					<view>
						<uni-easyinput v-model="form.totalNum" disabled="" />
					</view>
				</uni-forms-item>
				<uni-forms-item :label="`${$t('store.form.warehouse')}`">
					<view>
						<uni-easyinput v-model="form.warehouse" disabled="" />
					</view>
				</uni-forms-item>
				<uni-forms-item :label="`${$t('store.form.shelfName')}`">
					<view>
						<uni-easyinput v-model="form.shelfName" disabled="" />
					</view>
				</uni-forms-item>
			</uni-forms>
		</view>
		<view class="bottom-btn">
			<button class="button" type="primary" @click="store">{{ $t('store.form.btn') }}</button>

		</view>
		<view>
			<keyboard-listener @keydown="scancodelisten"></keyboard-listener>
		</view>
	</view>
</template>

<script>
import api from '../../common/api.js'
import common from '../../common/common.js'
import UniNumberBox from '@/components/uni-number-box/uni-number-box.vue'
import KeyboardListener from 'uniapp-keyboard-listener'

import { reactive, ref, nextTick } from "vue";
import { onLoad, onShow, onUnload, onHide, onBackPress } from "@dcloudio/uni-app";
export default {
	components: {
		UniNumberBox,
		KeyboardListener
	},
	data() {
		return {
			scannerBuffer: '',
			timer: null,
			index: 0,
			rules: {},
			id: '',
			batchnumber: '', //批次号
			table: [],
			isFocus: true,
			numFocus: false,
			warehouseList: [],
			aaaa: true,
			pdacode: '',
			form: {
				id: '',
				code: '',
				name: '',
				warehouse: '',
				num: 1,
				batchnumber: '123',
				date: '2024-10-10',
				storeNum: 0,
				totalNum: 20,
				inboundOrderId: '',
				expiryDate: ''
			},
			code: '',
			changeInput: false,
		}
	},
	onNavigationBarButtonTap(e) {
		console.log(e, '----e---')
		this.code = ''
		let that = this
		uni.scanCode({
			success: async function (res) {
				console.log('条码类型：' + res.scanType);
				console.log('条码内容：' + res.result);
				that.code = res.result
				await that.sendCode()
			}
		});
	},


	async onLoad(option) {
		console.log('上一个页面传递过来的参数', option)
		this.form.id = option.id
		console.log('id', option.id)
		console.log('item', option.item)
		if (this.form.id !== undefined) {
			await this.findbyId()
		} else {
			uni.navigateBack()
		}

	},
	async onUnload() {
		clearInterval(this.timer);
		this.timer = null
	},
	methods: {
		async scancodelisten(e) {
			let keyCode = e.keyCode
			console.log(e, '-----e---')
			if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122)) {
				console.log('字母输入:', e.key);
				this.pdacode = this.pdacode + e.key
			} else if (keyCode >= 48 && keyCode <= 57) {
				console.log('数字输入:', e.key);
				console.log(this.numFocus, 'numFocus');
				this.pdacode = this.pdacode + e.key
			} else if (keyCode === 13) {
				console.log('pdaCode: ', this.pdacode);
				this.code = this.pdacode
				if(this.code!==''){
					await this.sendCode()
				}
				this.pdacode = ''
			} else {
				console.log('其他输入:', e.key);
			}

			console.log(this.pdacode, '-----this.pdacode---')
		},
		numchange(e) {
			console.log(e,'---e---');
			
			this.form.num = Number(e)
			console.log(this.form.num, '--numchange--this.form.num--')
		},
		async findbyId() {
			uni.showLoading()
			console.log(this.form.id + '----id');
			let ret = await common.scanInboundCode(this.form.id)
			console.log(ret, '------ret')
			if (ret.code === 0 && ret.data) {
				this.form.id = ret.data.id
				this.form.name = ret.data.drugName
				this.form.batchnumber = ret.data.batchNumber
				this.form.warehouse = ret.data.warehouseName
				this.form.shelfName = ret.data.shelfName
				this.form.storeNum = ret.data.confirmNum
				this.form.totalNum = ret.data.num
				this.form.inboundOrderId = ret.data.inboundOrderId
				let dateFormat = common.formatDate(ret.data.expiryDate)
				this.form.expiryDate = dateFormat
				uni.hideLoading()
			} else {
				uni.showToast({
					title: ret.message,
					icon: 'none'
				});
				uni.hideLoading()
				setTimeout(() => {
					uni.navigateBack()
				}, 2000)
			}

		},
		formfocus(e) {
			this.pdacode = ''
			setTimeout(() => {
				this.isFocus = false
				this.numFocus = true
			}, 0)
		},
		formblur(e) {
			console.log(e.detail.value)
			this.pdacode = ''
			this.numFocus = false;
		},
		async store() {
			uni.showLoading()
			let ret = await api.batchInbound(
				{
					"inboundOrderDetailId": this.form.id, //入库单明细id
					"totalNum": this.form.num, //入库数量
				}
			)
			console.log('store---ret--',ret)
			if (ret.code === 0 && ret.success) {
				uni.hideLoading()
				uni.navigateBack()
			} else {
				uni.hideLoading()
				this.$nextTick(()=>{
					uni.showToast({
						title: ret.msg,
						icon: 'none'
					});
				})
				
			}
		},
		async sendCode() {
			uni.showLoading()
			let ret = await common.scanInboundCode(this.code)
			if (ret.code === 0 && ret.data) {
				let checkName = ret.data.drugName === this.form.name
				let checkWarehouseName = ret.data.warehouseName === this.form.warehouse
				let checkshelfName = ret.data.shelfName === this.form.shelfName
				let checkinboundOrderId = ret.data.inboundOrderId === this.form.inboundOrderId
				if (checkName && checkWarehouseName && checkshelfName && checkinboundOrderId) {
					this.form.num++
					uni.hideLoading()
				} else {
					uni.hideLoading()
					this.$nextTick(()=>{
						uni.showToast({
							title: this.$t('scan.dataerror'),
							icon: 'none'
						});
					})
					
				}
			} else {
				uni.hideLoading()
				this.$nextTick(()=>{
					uni.showToast({
						title: ret.message,
						icon: 'none'
					});
				})
				
			}
			console.log('---hideLoading--');
			let that = this
			setTimeout(() => {
				this.changeInput = false;
				this.isFocus = true;
			}, 0)
		}

	}
}
</script>

<style>

.form {
	margin-bottom: 40rpx;
}

.bottom-btn {
	position: absolute;
	width: 100%;
	bottom: 0;
}

.code-item {
	display: flex;
	flex: 1;

}

.jk-footer {
	position: absolute;
	top: 0;
	/* z-index: -99999; */
}
</style>
